package com.example.statistics.schedule;

import com.example.statistics.service.DailyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Component
public class ScheduledTask {
    @Autowired
    private DailyService dailyService;
//  0 0 1 * * ?
    //0/5 * * * * ? 表示间隔5秒执行一次
    //每天凌晨一点获取前一天的统计数据
    @Scheduled(cron = "0 0 1 * * ?")
    public void census(){
        //设置日期格式
        DateFormat dateFormat =new SimpleDateFormat("yyyy-MM-dd");
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(new Date());
        rightNow.add(Calendar.DAY_OF_YEAR,-1);
        Date time = rightNow.getTime();
        String date =dateFormat.format(time);
        dailyService.registerCount(date);
    }
}
